package com.jfl.web.controller.system;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jfl.annotation.Log;
import com.jfl.base.AbstractController;
import com.jfl.common.Constants;
import com.jfl.core.utils.ShiroUtils;
import com.jfl.enums.LogType;
import com.jfl.enums.Module;
import com.jfl.exception.BusinessException;
import com.jfl.model.SysUser;
import com.jfl.system.SysUserService;
import com.jfl.utils.EncryptUtils;
import com.jfl.utils.FileUploadUtil;

/**
 * 个人资料修改控制器
 * 
 * @author 赵炎
 * @version [V1.00, 2018年12月12日]
 * @since V1.00
 */
@Controller
@RequestMapping("${jfl.module.system}/user/profile")
public class SysProfileController extends AbstractController
{
    @Reference(version = "${jfl.version}")
    private SysUserService sysUserService;
    
    @Value("${jfl.image.path}")
    private String avatarPath;
    
    @Value("${jfl.demoMode}")
    private boolean mode;
    /**
     * 个人信息 跳转页面
     * 
     * @param modelMap
     * @return
     */
    @GetMapping
    public String profile(ModelMap modelMap)
    {
        modelMap.put("user", ShiroUtils.getCurrentUser());
        return Constants.MODULE_SYS_USER_PROFILE_PREFIX + "profile";
    }
    
    /**
     * 重置密码 跳转页面
     * 
     * @param modelMap
     * @return
     */
    @GetMapping("/resetPwd/{userId}")
    public String resetPwd(@PathVariable("userId") Long userId, ModelMap modelMap)
    {
        modelMap.put("user", ShiroUtils.getCurrentUser());
        return Constants.MODULE_SYS_USER_PROFILE_PREFIX + "resetPwd";
    }
    
    /**
     * 重置密码
     * 
     * @param user
     * @return
     */
    @Log(module = Module.USER, value = "个人重置密码", type = LogType.UPDATE)
    @PostMapping("/resetPwd")
    @ResponseBody
    public ResponseEntity<ModelMap> resetPwd(SysUser user)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        SysUser s = ShiroUtils.getCurrentUser();
        s.setSalt(EncryptUtils.randomSalt());
        s.setPassword(EncryptUtils.encryptPassword(s.getUserName(), s.getPassword(), s.getSalt()));
        this.sysUserService.update(s);
        ShiroUtils.setCurrentUser(this.sysUserService.selectByUserName(s.getUserName()));
        return setSuccessModelMap();
    }
    @GetMapping("/resetPwd")
    public String resetPwd(ModelMap mmap)
    {
        SysUser user = ShiroUtils.getCurrentUser();
        mmap.put("user", this.sysUserService.getById(user.getId()));
        return Constants.MODULE_SYS_USER_PROFILE_PREFIX + "resetPwd";
    }
    
    /**
     * 修改用户跳转页面
     * 
     * @param userId
     * @param mmap
     * @return
     */
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap modelMap)
    {
        modelMap.put("user", ShiroUtils.getCurrentUser());
        return Constants.MODULE_SYS_USER_PROFILE_PREFIX + "edit";
    }
    
    /**
     * 修改头像 跳转页面
     * 
     * @param userId
     * @param mmap
     * @return
     */
    @GetMapping("/avatar")
    public String avatar(ModelMap modelMap)
    {
        modelMap.put("user", ShiroUtils.getCurrentUser());
        return "system/user/profile/" + "avatar";
    }
    
    /**
     * 修改头像
     * 
     * @param user
     * @param file
     * @return
     */
    @Log(module = Module.USER, value = "修改个人头像", type = LogType.UPDATE)
    @PostMapping("/updateAvatar")
    @ResponseBody
    public ResponseEntity<ModelMap> updateAvatar(@RequestParam("avatarfile") MultipartFile file)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        SysUser user = ShiroUtils.getCurrentUser();
        try
        {
            if (!file.isEmpty())
            {
                String avatar = FileUploadUtil.upload(avatarPath + "avatar/", file);
                user.setAvatar(avatar);
                sysUserService.update(user);
                ShiroUtils.setCurrentUser(user);
            }
        }
        catch (Exception e)
        {
            logger.error("修改头像失败", e);
            throw new BusinessException("修改头像失败");
        }
        return setSuccessModelMap();
    }
    
    @Log(module = Module.USER, value = "修改个人信息", type = LogType.UPDATE)
    @PostMapping("/update")
    @ResponseBody
    public ResponseEntity<ModelMap> update(SysUser user)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        this.sysUserService.update(user);
        ShiroUtils.setCurrentUser(this.sysUserService.selectByUserName(user.getUserName()));
        return setSuccessModelMap();
    }

}
